package LeetCode;

public class LC_014_LongestCommonPrefix {
    public static void main(String[] args) {
        String[] a = {"abcdwww", "abcoooo"};
        System.out.println(longestCommonPrefix(a));
    }

    private static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0)
            return "";
        StringBuilder res = new StringBuilder();
        int index = 0;
        int minLen = minlen(strs);
        while (index < minLen) {
            for (int i = 1; i < strs.length; i++) {
                if (strs[i].charAt(index) != strs[0].charAt(index))
                    return res.toString();
            }
            res.append(strs[0].charAt(index));
            index++;
        }
        return res.toString();
    }

    private static int minlen(String[] strs) {
        int min = Integer.MAX_VALUE;
        for (String str : strs)
            min = Math.min(min, str.length());
        return min;
    }
}